package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.mapper.ShoppingCartMapper;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Jiang
 * @date 2022/2/19
 */
@Service
@Slf4j
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart>
        implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper mapper;


    @Override
    public Page<ShoppingCart> selectByPage(Integer page, Integer pageSize) {
        Page<ShoppingCart> pages = new Page<>(page, pageSize);
        LambdaQueryWrapper<ShoppingCart> scqw = Wrappers.lambdaQuery(ShoppingCart.class);
        //填充用户id作为条件
        scqw.eq(ShoppingCart::getUserId, BaseContext.getId());
        mapper.selectPage(pages, scqw);
        return pages;
    }

    @Override
    public List<ShoppingCart> selectByList() {
        LambdaQueryWrapper<ShoppingCart> scqw = Wrappers.lambdaQuery(ShoppingCart.class);
        scqw.eq(ShoppingCart::getUserId,BaseContext.getId());
        scqw.orderByDesc(ShoppingCart::getCreateTime);
        return mapper.selectList(scqw);
    }

    @Override
    public ShoppingCart addShoppingCart(ShoppingCart shoppingCart) {
        //获取
        LambdaQueryWrapper<ShoppingCart> scqw = Wrappers.lambdaQuery(ShoppingCart.class);
        //指定用户id
        scqw.eq(ShoppingCart::getUserId, BaseContext.getId());
        //指定dishid
        if (shoppingCart.getDishId() != null) {
            scqw.eq(
                    ShoppingCart::getDishId, shoppingCart.getDishId());
        } else {
            //指定setmealid
            scqw.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }

        //查询
        ShoppingCart shoppingCart1 = mapper.selectOne(scqw);
        //判断是否为null，为null就添加
        if (shoppingCart1 == null) {
            //填充id
            shoppingCart.setUserId(BaseContext.getId());
            //设置数量
            shoppingCart.setNumber(1);
            //添加
            mapper.insert(shoppingCart);
            return shoppingCart;
        }
        shoppingCart1.setNumber(shoppingCart1.getNumber() + 1);
        mapper.updateById(shoppingCart1);
        return shoppingCart1;
    }

    @Override
    public ShoppingCart delShoppingCart(ShoppingCart shoppingCart) {
        //获取当前商品
        //获取
        LambdaQueryWrapper<ShoppingCart> scqw = Wrappers.lambdaQuery(ShoppingCart.class);
        //指定用户id
        scqw.eq(ShoppingCart::getUserId, BaseContext.getId());
        //指定dishid
        if (shoppingCart.getDishId() != null) {
            scqw.eq(shoppingCart.getDishId() != null,
                    ShoppingCart::getDishId, shoppingCart.getDishId());
        } else {
            //指定setmealid
            scqw.eq(shoppingCart.getSetmealId() != null,
                    ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }
        //查询
        shoppingCart = mapper.selectOne(scqw);
        //获取当前数量
        Integer number = shoppingCart.getNumber();
        if (number == 1) {
            //删除商品
            mapper.deleteById(shoppingCart.getId());
            shoppingCart.setNumber(number - 1);
        } else {
            shoppingCart.setNumber(number - 1);
            //修改
            mapper.updateById(shoppingCart);
        }
        return shoppingCart;
    }

    @Override
    public void clearShoppingCart() {
        LambdaQueryWrapper<ShoppingCart> lqw = Wrappers.lambdaQuery(ShoppingCart.class);
        //此用户购物车的所有信息
        lqw.eq(ShoppingCart::getUserId,BaseContext.getId());
        mapper.delete(lqw);
    }
}
